/**
 * 
 */
package br.com.ctm.guiamedico.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import br.com.ctm.guiamedico.dao.api.MedicoDao;
import br.com.ctm.guiamedico.model.Medico;
import br.com.framework.persistencia.impl.BaseDaoImpl;
import br.com.framework.persistencia.model.AuditedEntity.Status;

/**
 * @author Cleber
 *
 */
@Stateless
public class MedicoDaoImpl extends BaseDaoImpl<Medico> implements MedicoDao{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 */
	public MedicoDaoImpl() {
		super(Medico.class);
	}
	
	/**
	 * @param isQueryCount
	 * @param numeroVersao
	 * @return
	 */
	private Query createQueryMedicosIncluidosNaVersao(boolean isQueryCount, Long numeroVersao) {
		StringBuffer jpql = new StringBuffer();
		if (isQueryCount) {
			jpql.append("SELECT COUNT(m) FROM ");
		} else {
			jpql.append("SELECT m FROM ");
		}
		jpql.append("Medico AS m, Versao AS v ");
		jpql.append("WHERE v.numero = :numeroVersao AND v.status = :active ");
		jpql.append("AND m.dataCriacao <= v.dataCriacao ");
		jpql.append("AND m.status = :active ");
		if (!isQueryCount) {
			jpql.append("ORDER BY m.nome ASC");
		}
		Query query = getEntityManager().createQuery(jpql.toString(), Medico.class);
		query.setParameter("numeroVersao", numeroVersao);
		query.setParameter("active", Status.ACTIVE);
		
		return query;
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.dao.api.MedicoDao#getMedicosIncluidosNaVersao(java.lang.Long, int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Medico> getMedicosIncluidosNaVersao(Long numeroVersao,
			int firstResult, int maxResults) throws PersistenceException {
		Query query = createQueryMedicosIncluidosNaVersao(false, numeroVersao);
		if (firstResult > -1 && maxResults > -1) {
			query.setFirstResult(firstResult);
			query.setMaxResults(maxResults);
		}
		return query.getResultList();
	}
	
	/**
	 * @param isQueryCount
	 * @param numeroVersao
	 * @return
	 */
	private Query createQueryMedicosRemovidosNaVersao(boolean isQueryCount, Long numeroVersao) {
		StringBuffer jpql = new StringBuffer();
		if (isQueryCount) {
			jpql.append("SELECT COUNT(m) FROM ");
		} else {
			jpql.append("SELECT m FROM ");
		}
		jpql.append("Medico AS m, Versao AS v ");
		jpql.append("WHERE v.numero = :numeroVersao AND v.status = :active ");
		jpql.append("AND m.dataCriacao <= v.dataCriacao ");
		jpql.append("AND m.dataModificacao <= v.dataCriacao ");
		jpql.append("AND m.status = :inactive ");
		if (!isQueryCount) {
			jpql.append("ORDER BY m.nome ASC");
		}
		Query query = getEntityManager().createQuery(jpql.toString(), Medico.class);
		query.setParameter("numeroVersao", numeroVersao);
		query.setParameter("active", Status.ACTIVE);
		query.setParameter("inactive", Status.INACTIVE);
		
		return query;
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.dao.api.MedicoDao#getMedicosRemovidosNaVersao(java.lang.Long, int, int)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Medico> getMedicosRemovidosNaVersao(Long numeroVersao,
			int firstResult, int maxResults) throws PersistenceException {
		Query query = createQueryMedicosRemovidosNaVersao(false, numeroVersao);
		if (firstResult > -1 && maxResults > -1) {
			query.setFirstResult(firstResult);
			query.setMaxResults(maxResults);
		}
		return query.getResultList();
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.dao.api.MedicoDao#getCountMedicosIncluidosNaVersao(java.lang.Long)
	 */
	@Override
	public Long getCountMedicosIncluidosNaVersao(Long numeroVersao)
			throws PersistenceException {
		Query query = createQueryMedicosIncluidosNaVersao(true, numeroVersao);
		return (Long) query.getSingleResult();
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.guiamedico.dao.api.MedicoDao#getMedicosRemovidosNaVersao(java.lang.Long)
	 */
	@Override
	public Long getCountMedicosRemovidosNaVersao(Long numeroVersao)
			throws PersistenceException {
		Query query = createQueryMedicosRemovidosNaVersao(true, numeroVersao);
		return (Long) query.getSingleResult();
	}
	
	
}
